package com.xindaoapp.happypet.view;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.CommonLog;
import com.xindaoapp.happypet.utils.CommonUtil;

/**
 * 下拉刷新控件
 * 
 * @author xindaoapp
 */
public class PullToRefreshListView extends ListView implements OnScrollListener {

    private final CommonLog log = new CommonLog();

    public final static int RELEASE_To_REFRESH = 0;
    public final static int PULL_To_REFRESH = 1;
    public final static int REFRESHING = 2;
    public final static int DONE = 3;
    public final static int LOADING = 4;

    private final static int RATIO = 3;

    private LayoutInflater inflater;

    private LinearLayout headView;

    private TextView tipsTextview;
    private TextView lastUpdatedTextView;
    private ImageView arrowImageView;
    // private ProgressBar progressBar;
    private boolean isScroll2Bottom = false; // 是否滚动到底部
    private boolean isLoadMoving = false; // 是否正在加载更多中
    private View footerView; // 脚布局
    private int footerViewHeight; // 脚布局的高度

    private AnimationDrawable animation;
    private RotateAnimation reverseAnimation;

    private boolean isRecored;

    private int headContentWidth;
    private int headContentHeight;

    private int startY;
    private int firstItemIndex;

    private int state;

    private boolean isBack;

    private OnPullDownRefreshListener refreshListener;

    private boolean isRefreshable = true;

    private String lastUpdateTime = new String();

    public PullToRefreshListView(Context context) {
        super(context);
        init(context);
        // setOverScrollMode(View.OVER_SCROLL_NEVER);
    }

    public PullToRefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
        // setOverScrollMode(View.OVER_SCROLL_NEVER);
    }

    public PullToRefreshListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
        // setOverScrollMode(View.OVER_SCROLL_NEVER);
    }

    /** 初始化脚布局 */
    private void initFooter() {
        footerView = LayoutInflater.from(getContext()).inflate(R.layout.listview_footer, null);

        measureView(footerView); // 测量一下脚布局的高度

        footerViewHeight = footerView.getMeasuredHeight();

        footerView.setPadding(0, -footerViewHeight, 0, 0); // 隐藏脚布局
        footerView.setClickable(false);
        this.addFooterView(footerView);
    }

    private void init(Context context) {
        setCacheColorHint(context.getResources().getColor(R.color.transparent));
        inflater = LayoutInflater.from(context);

        this.setFadingEdgeLength(0);

        headView = (LinearLayout) inflater.inflate(R.layout.mylistview_head, null);

        arrowImageView = (ImageView) headView.findViewById(R.id.head_arrowImageView);
        // arrowImageView.setMinimumWidth(70);
        // arrowImageView.setMinimumHeight(50);
        // progressBar = (ProgressBar) headView.findViewById(R.id.head_ic_progress);

        tipsTextview = (TextView) headView.findViewById(R.id.head_tipsTextView);
        lastUpdatedTextView = (TextView) headView.findViewById(R.id.head_lastUpdatedTextView);

        measureView(headView);
        headContentHeight = headView.getMeasuredHeight();
        headContentWidth = headView.getMeasuredWidth();

        headView.setPadding(0, -1 * headContentHeight, 0, 0);
        // headView.setPadding(0, DisplayUtil.dip2px(getContext(), 10), 0,
        // DisplayUtil.dip2px(getContext(), 10));
        headView.invalidate();

        log.i("head view size------width:" + headContentWidth + " height:" + headContentHeight);
        headView.setClickable(false);
        addHeaderView(headView, null, false);
        setOnScrollListener(this);

        arrowImageView.setBackgroundResource(R.anim.pulltoreference_animation);

        animation = (AnimationDrawable) arrowImageView.getBackground();
        animation.setOneShot(false);
        /*
         * animation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f,
         * RotateAnimation.RELATIVE_TO_SELF, 0.5f); animation.setInterpolator(new
         * LinearInterpolator()); animation.setDuration(250); animation.setFillAfter(true);
         * 
         * reverseAnimation = new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f,
         * RotateAnimation.RELATIVE_TO_SELF, 0.5f); reverseAnimation.setInterpolator(new
         * LinearInterpolator()); reverseAnimation.setDuration(200);
         * reverseAnimation.setFillAfter(true);
         */

        state = DONE;
        isRefreshable = true;

        // lastUpdateTime = formatDate(new Date());
        lastUpdatedTextView.setText("最近更新:\t" + CommonUtil.formatPullToRefreshDate());

        initFooter();
    }

    @Override
    public void onScroll(AbsListView arg0, int firstVisiableItem, int arg2, int arg3) {
        firstItemIndex = firstVisiableItem;
        if ((firstVisiableItem + arg2) >= arg3 - 1 && arg3 > 0) {
            isScroll2Bottom = true;
        } else {
            isScroll2Bottom = false;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView arg0, int scrollState) {
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
                || scrollState == OnScrollListener.SCROLL_STATE_FLING) {
            if (isScroll2Bottom && !isLoadMoving) { // 滚动到底部
                // 加载更多
                footerView.setPadding(0, 0, 0, 0);
                this.setSelection(this.getCount()); // 滚动到ListView的底部
                isLoadMoving = true;

                if (refreshListener != null) {
                    refreshListener.onLoadMoring();
                }
            }
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isRefreshable) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (firstItemIndex == 0 && !isRecored) {
                        isRecored = true;
                        startY = (int) event.getY();
                    }
                    break;

                case MotionEvent.ACTION_UP:

                    if (state != REFRESHING && state != LOADING) {
                        if (state == DONE) {
                        }
                        if (state == PULL_To_REFRESH) {
                            state = DONE;
                            changeHeaderViewByState();
                        }
                        if (state == RELEASE_To_REFRESH) {
                            state = REFRESHING;
                            changeHeaderViewByState();
                            onRefresh();
                        }
                    }

                    isRecored = false;
                    isBack = false;

                    break;

                case MotionEvent.ACTION_MOVE:
                    int tempY = (int) event.getY();

                    if (!isRecored && firstItemIndex == 0) {
                        isRecored = true;
                        startY = tempY;
                    }

                    if (state != REFRESHING && isRecored && state != LOADING) {

                        if (state == RELEASE_To_REFRESH) {

                            setSelection(0);

                            if (((tempY - startY) / RATIO < headContentHeight)
                                    && (tempY - startY) > 0) {
                                state = PULL_To_REFRESH;
                                changeHeaderViewByState();

                            } else if (tempY - startY <= 0) {
                                state = DONE;
                                changeHeaderViewByState();

                            } else {
                            }
                        }
                        if (state == PULL_To_REFRESH) {

                            setSelection(0);

                            if ((tempY - startY) / RATIO >= headContentHeight) {
                                state = RELEASE_To_REFRESH;
                                isBack = true;
                                changeHeaderViewByState();

                            } else if (tempY - startY <= 0) {
                                state = DONE;
                                changeHeaderViewByState();

                            }
                        }

                        if (state == DONE) {
                            if (tempY - startY > 0) {
                                state = PULL_To_REFRESH;
                                changeHeaderViewByState();
                            }
                        }

                        if (state == PULL_To_REFRESH) {
                            headView.setPadding(0, -1 * headContentHeight + (tempY - startY)
                                    / RATIO, 0, 0);

                        }

                        if (state == RELEASE_To_REFRESH) {
                            headView.setPadding(0, (tempY - startY) / RATIO - headContentHeight, 0,
                                    0);
                        }

                    }

                    break;
            }
        }

        return super.onTouchEvent(event);
    }

    /** 当刷新任务执行完毕时, 回调此方法, 去刷新界面 */
    public void onRefreshFinish() {
        if (isLoadMoving) { // 隐藏脚布局
            isLoadMoving = false;
            isScroll2Bottom = false;
            footerView.setPadding(0, -footerViewHeight, 0, 0);
        }
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public void changeHeaderViewByState() {
        switch (state) {
            case RELEASE_To_REFRESH:
                // arrowImageView.setVisibility(View.VISIBLE);
                // progressBar.setVisibility(View.GONE);
                tipsTextview.setVisibility(View.VISIBLE);
                lastUpdatedTextView.setVisibility(View.VISIBLE);

                tipsTextview.setText(R.string.pull_to_refresh_from_bottom_release_label);

                break;
            case PULL_To_REFRESH:
                // progressBar.setVisibility(View.GONE);
                tipsTextview.setVisibility(View.VISIBLE);
                lastUpdatedTextView.setVisibility(View.VISIBLE);
                if (isBack) {
                    isBack = false;

                    tipsTextview.setText(R.string.pull_to_refresh_from_bottom_pull_label);
                } else {
                    tipsTextview.setText(R.string.pull_to_refresh_from_bottom_pull_label);
                }
                break;

            case REFRESHING:
                headView.setPadding(0, 0, 0, 0);

                // progressBar.setVisibility(View.VISIBLE);
                if (animation != null && !animation.isRunning()) {
                    animation.start();
                }
                tipsTextview.setText(R.string.pull_to_refresh_from_bottom_refreshing_label);
                lastUpdatedTextView.setVisibility(View.VISIBLE);

                break;
            case DONE:
                headView.setPadding(0, -1 * headContentHeight, 0, 0);
                if (animation != null && animation.isRunning()) {
                    animation.stop();
                }
                // progressBar.setVisibility(View.GONE);
                tipsTextview.setText(R.string.pull_to_refresh_from_bottom_refreshing_label);
                lastUpdatedTextView.setVisibility(View.VISIBLE);

                break;
        }
    }

    public void setonRefreshListener(OnPullDownRefreshListener refreshListener) {
        this.refreshListener = refreshListener;
        isRefreshable = true;
    }

    public interface OnPullDownRefreshListener {
        public void onRefresh();

        /** 当加载更多时回调 当加载更多完毕之后, 需要手动的调用onRefreshFinish(), 去隐藏脚布局 */
        public void onLoadMoring();
    }

    public void onRefreshComplete() {
        state = DONE;
        lastUpdateTime = formatDate(new Date());
        lastUpdatedTextView.setText("最近更新:\t" + CommonUtil.formatPullToRefreshDate());
        changeHeaderViewByState();
    }

    @SuppressLint("SimpleDateFormat")
    public static String formatDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = format.format(date);

        return str;
    }

    private void onRefresh() {
        if (refreshListener != null) {
            refreshListener.onRefresh();
        }
    }

    private void measureView(View child) {
        ViewGroup.LayoutParams p = child.getLayoutParams();
        if (p == null) {
            p =
                    new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
        int lpHeight = p.height;
        int childHeightSpec;
        if (lpHeight > 0) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(childWidthSpec, childHeightSpec);
    }

    public void setAdapter(BaseAdapter adapter) {
        super.setAdapter(adapter);
    }

    public boolean isRefreshable() {
        return isRefreshable;
    }

    public void setRefreshable(boolean isRefreshable) {
        this.isRefreshable = isRefreshable;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        try {
            super.dispatchDraw(canvas);
        } catch (Exception e) {
            Log.i("PullToRefreshListView", "balabala");
        }
    }
}
